var util = require('../../utils/util.js');
var app = getApp();
Page({
  data: {
    hasShop: false,
    shopList: '', //门店列表
    address_id: '', //门店id
    flag: 0, //0是我的过来的，1是确认订单过来的

  },
  onLoad: function(e) {
    console.log(e);
    this.data.flag = e.flag; //判断是哪过来的
  },
  onShow: function(e) {

    var that = this;
    util.postUrl('member_address/address_list', {}, function(res) {
      if (res.data.code === 200) {
        var arr = res.data.datas;
        for (var i = 0; i < arr.length; i++) {
          that.setData({
            hasShop: true,
            shopList: res.data.datas,
            address_id: arr[i].address_id
          })
        }
      } else {
        return false;
      }
    })
  },
  // 选择默认地址
  click: function(e) {
    console.log('1111112', e.currentTarget.dataset.id);
    util.postUrl('member_address/default_address', {
      address_id: e.currentTarget.dataset.id
    }, function(res) {
      if (res.data.code == 200) {
        wx.showToast({
          title: '默认地址设置成功',
          icon: 'none'
        })
      }
    })
  },
  // 删除地址：
  detele: function(e) {
    var that = this;
    let index = e.currentTarget.dataset.index;
    let shopList = this.data.shopList;
    util.postUrl('member_address/remove_address', {
      address_id: shopList[index].address_id
    }, function(res) {
      if (res.data.code === 200) {
        shopList.splice(index, 1)
        that.setData({
          shopList: shopList
        });
        if (!shopList.length) {
          that.setData({
            hasShop: false
          });
        }
      }
    })
  },
  // 新建门店
  addShop: function() {
    wx.navigateTo({
      url: '../auth-info/auth-info',
    })
  },
  addressEditor: function(e) {
    let item = e.currentTarget.dataset.item;
    wx.navigateTo({
      url: '../auth-info/auth-info?item=' + JSON.stringify(item),
    })
  },
  chooseAddress: function(e) {
    if (this.data.flag == 1) {
      let item = e.currentTarget.dataset.item;
      app.address_info = item;
      console.log(item);
      wx.navigateBack({
        
      })
    }

  }
})